// 单指针解法
function ts_main1(nums: number[]): number[] {
  let index = 0;
  const len = nums.length;
  // 查找非零数字并移动
  for (let i = 0; i < len; i++) {
    if (nums[i] !== 0) {
      nums[index] = nums[i];
      index++;
    }
  }
  // 赋值零
  for ( ; index < len; index++) {
    nums[index] = 0;
  }
  return nums;
}

console.log(ts_main1([0, 1, 0, 3, 12]));

function ts_swap(nums: number[], left: number, right: number): void {
  // if (left === right) return;
  const temp = nums[left];
  nums[left] = nums[right];
  nums[right] = temp;
}

// 双指针
function ts_main2(nums: number[]): number[] {
  let firstZeroIndex = 0;
  const len = nums.length;
  for (let i = 0; i < len; i++) {
    if (nums[i] !== 0) {
      ts_swap(nums, firstZeroIndex, i);
      firstZeroIndex++;
    }
  }
  return nums;
}

console.log(ts_main2([0, 1, 0, 3, 12]));
